Syntax of Conditionals

Contents|Index|Previous|Next
Syntax of conditionals

The syntax of a simple conditional with no else is as follows.

conditional-directive
text-if-true
endif

The text-if-true may be any lines of text, to be considered as part of the makefile if the condition is true. If the condition is false, no text is used instead. The syntax of a complex conditional is as follows.

conditional-directive
text-if-true
else
text-if-false
endif

If the condition is true, text-if-true is used; otherwise, text-if-false is used instead.

The text-if-false can be any number of lines of text.

The syntax of the conditional-directive is the same whether the conditional is simple or complex. There are four different directives that test different conditions. The following is a description of them.

ifeq (arg1, arg2)
ifeq arg1 arg2
ifeq arg1 arg2
ifeq arg1 arg2
ifeq arg1 arg2

Expand all variable references in arg1 and arg2 and compare them. If they are identical, the text-if-true is effective; otherwise, the text-if-false, if any, is effective.

Often you want to test if a variable has a non-empty value.

When the value results from complex expansions of variables and functions, expansions you would consider empty may actually contain whitespace characters and thus are not seen as empty.

However, you can use the strip function (see Functions for string substitution and analysis) to avoid interpreting whitespace as a non-empty value.

For example, the following will evaluate text-if-empty even if the expansion of $(foo) contains whitespace characters.

ifeq ($(strip $(foo)),)
text-if-empty
endif

ifneq ( arg1, arg2)
ifneq arg1 arg2
ifneq arg1 arg2
ifneq arg1 arg2
ifneq arg1 arg2

Expand all variable references in arg1 and arg2 and compare them.

If they differ, the text-if-true is effective; otherwise, the text-if-false, if any, is effective.

ifdef variable-name

If the variable variable-name has a non-empty value, the text-if-true is effective; otherwise, the text-if-false, if any, is effective. Variables that have never been defined have an empty value.

Note:
ifdef only tests whether a variable has a value. It does not expand the variable to see if that value is nonempty. Consequently, tests using ifdef return true for all definitions except those like foo =.

To test for an empty value, use ifeq ($(foo),), as in the following example.

bar =
foo = $(bar)
ifdef foo
frobozz = yes
else
frobozz = no
endif

The previous definition example sets frobozz to yes while the following definition sets frobozz tono.

foo =
ifdef foo
frobozz = yes
else
frobozz = no
endif

ifndef variable-name

If the variable variable-name has an empty value, the text-if-true is effective; otherwise, the text-if-false, if any, is effective.

Extra spaces are allowed and ignored at the beginning of the conditional directive line, but a tab is not allowed. (If the line begins with a tab, it will be considered a command for a rule.) Aside from this, extra spaces or tabs may be inserted with no effect anywhere except within the directive name or within an argument. A comment starting with # may appear at the end of the line.

The other two directives that play a part in a conditional are else and endif. Each of these directives is written as one word, with no arguments. Extra spaces are allowed and ignored at the beginning of the line, and spaces or tabs at the end. A comment starting with # may appear at the end of the line.

Conditionals affect which lines of the makefile make uses. If the condition is true, make reads the lines of the text-if-true as part of the makefile; if the condition is false, make ignores those lines completely. It follows that syntactic units of the makefile, such as rules, may safely be split across the beginning or the end of the conditional.

make evaluates conditionals when it reads a makefile. Consequently, you cannot use automatic variables in the tests of conditionals because they are not defined until commands are run (see Automatic variables). To prevent intolerable confusion, it is not permitted to start a conditional in one makefile and end it in another. However, you may write an include directive within a conditional, provided you do not attempt to terminate the conditional inside the included file.

Top|Contents|Index|Previous|Next